<?php
/* -------------------------------------------------------------------------------------
* 	ID:						$Id: stats_sales_report.php 119 2013-09-21 12:09:36Z phone.mueller@googlemail.com $
* 	Letzter Stand:			$Revision: 119 $
* 	zuletzt geaendert von:	$Author: siekiera $
* 	Datum:					$Date: 2013-09-21 12:09:36 +0000 (Sat, 21 Sep 2013) $
*
* 	SEO:mercari by Siekiera Media
* 	http://www.seo-mercari.de
*
* 	Copyright (c) since 2011 SEO:mercari
* --------------------------------------------------------------------------------------
* 	based on:
* 	(c) 2000-2001 The Exchange Project  (earlier name of osCommerce)
* 	(c) 2002-2003 osCommerce - www.oscommerce.com
* 	(c) 2003     nextcommerce - www.nextcommerce.org
* 	(c) 2005     xt:Commerce - www.xt-commerce.com
*
* 	Released under the GNU General Public License
* --------------------------------------------------------------------------------------

possible views (srView):
  1 yearly
  2 monthly
  3 weekly
  4 daily

possible options (srDetail):
  0 no detail
  1 show details (products)
  2 show details only (products)

export
  0 normal view
  1 html view without left and right
  2 csv

sort
  0 no sorting
  1 product description asc
  2 product description desc
  3 #product asc, product descr asc
  4 #product desc, product descr desc
  5 revenue asc, product descr asc
  6 revenue desc, product descr des

* ------------------------------------------------------------------------------------ */

require('includes/application_top.php');

require(DIR_WS_CLASSES.'currencies.php');
$currencies = new currencies();

// default detail no detail
$srDefaultDetail = 2;
// default view (daily)
$srDefaultView = 4;
// default export
$srDefaultExp = 0;
// default sort
$srDefaultSort = 4;

if(($_GET['report']) && (not_null($_GET['report'])))
	$srView = $_GET['report'];

if($srView < 1 || $srView > 4)
	$srView = $srDefaultView;

if(($_GET['detail']) && (not_null($_GET['detail'])))
	$srDetail = $_GET['detail'];
else
	$srDetail = $srDefaultDetail;

if(($_GET['export']) && (not_null($_GET['export'])))
	$srExp = $_GET['export'];

if($srExp < 0 || $srExp > 2)
	$srExp = $srDefaultExp;

if(($_GET['max']) && (not_null($_GET['max'])))
	$srMax = $_GET['max'];

if(!is_numeric($srMax))
	$srMax = 0;

if(($_GET['status']) && (not_null($_GET['status'])))
	$srStatus = $_GET['status'];

if(!is_numeric($srStatus))
	$srStatus = 0;

if(($_GET['payment']) && (not_null($_GET['payment'])))
	$srPayment = $_GET['payment'];
else
	$srPayment = 0;

if(($_GET['sort']) && (not_null($_GET['sort'])))
	$srSort = $_GET['sort'];

if($srSort < 1 || $srSort > 6)
	$srSort = $srDefaultSort;

$startDate = "";
$startDateG = 0;
if(($_GET['startD']) && (not_null($_GET['startD']))) {
	$sDay = $_GET['startD'];
	$startDateG = 1;
} else
	$sDay = 1;

if(($_GET['startM']) && (not_null($_GET['startM']))){
	$sMon = $_GET['startM'];
	$startDateG = 1;
} else
	$sMon = 1;

if(($_GET['startY']) && (not_null($_GET['startY']))){
	$sYear = $_GET['startY'];
	$startDateG = 1;
} else
	$sYear = date("Y");

$startDate = ($startDateG ? mktime(0, 0, 0, $sMon, $sDay, $sYear) : mktime(0, 0, 0, date("m"), 1, date("Y")));

$endDate = "";
$endDateG = 0;
if(($_GET['endD']) && (not_null($_GET['endD']))) {
	$eDay = $_GET['endD'];
	$endDateG = 1;
} else
	$eDay = 1;

if(($_GET['endM']) && (not_null($_GET['endM']))) {
	$eMon = $_GET['endM'];
	$endDateG = 1;
} else
	$eMon = 1;

if(($_GET['endY']) && (not_null($_GET['endY']))) {
	$eYear = $_GET['endY'];
	$endDateG = 1;
} else
	$eYear = date("Y");

$endDate = ($endDateG ? mktime(0, 0, 0, $eMon, $eDay+1, $eYear) : mktime(0, 0, 0, date("m"), date("d")+1, date("Y")));

require(DIR_WS_CLASSES.'class.sales_report.php');
$sr = new sales_report($srView, $startDate, $endDate, $srSort, $srStatus, $srFilter, $srPayment);  
$startDate = $sr->startDate;
$endDate = $sr->endDate;  

require(DIR_WS_INCLUDES.'metatag.php');
?>
</head>
<body>
<?php require(DIR_WS_INCLUDES.'header.php'); ?>
<div id="wrapper">
<table class="outerTable" cellspacing="0" cellpadding="0">
  	<tr>
		<td class="columnLeft2" width="<?php echo BOX_WIDTH; ?>" valign="top">
			<?php require(DIR_WS_INCLUDES.'column_left.php'); ?>
		</td>
	    <td class="boxCenter" valign="top">
	      	<table border="0" width="100%" cellspacing="0" cellpadding="2">
	        	<tr>
	          		<td colspan=2>
	            		<table border="0" width="100%" cellspacing="0" cellpadding="0">
	  						<tr>
					    		<td class="pageHeading"><?php echo HEADING_TITLE; ?></td>
							</tr>
						</table>
	          		</td>
	        	</tr>
	        	<tr>
	          		<td colspan="2">
	          			<?php echo draw_form('sales_report', 'stats_sales_report.php', getMenuID('stats_sales_report'), 'GET'); ?>
							<table style="border: 1px solid; border-color: #cccccc;" width="100%" cellspacing="0" cellpadding="5">
	                			<tr>
									<td align="left" rowspan="2" class="menuBoxHeading">
										<input type="radio" name="report" value="1" <?php if($srView == 1) echo "checked"; ?>><?php echo REPORT_TYPE_YEARLY; ?><br />
										<input type="radio" name="report" value="2" <?php if($srView == 2) echo "checked"; ?>><?php echo REPORT_TYPE_MONTHLY; ?><br />
										<input type="radio" name="report" value="3" <?php if($srView == 3) echo "checked"; ?>><?php echo REPORT_TYPE_WEEKLY; ?><br />
										<input type="radio" name="report" value="4" <?php if($srView == 4) echo "checked"; ?>><?php echo REPORT_TYPE_DAILY; ?><br />
									</td>
									<td class="menuBoxHeading">
										<?php echo REPORT_START_DATE; ?><br />
										<select name="startD" size="1" style="width:auto">
											<?php
											$j = ($startDate ? date('j', $startDate) : 1);
											for ($i = 1; $i < 32; $i++)
												echo '<option'.($j == $i ? ' selected="selected"' : '').' value="'.$i.'">'.$i.'</option>';
											?>
										</select>
										
										<select name="startM" size="1" style="width:auto">
											<?php
											$m = ($startDate ? date("n", $startDate) : 1);
											for ($i = 1; $i < 13; $i++)
												echo '<option'.($m == $i ? ' selected="selected"' : '').' value="'.$i.'">'.strftime("%B", mktime(0, 0, 0, $i, 1)).'</option>';
											?>
										</select>

										<select name="startY" size="1" style="width:auto">
											<?php
											$y = ($startDate ? (date("Y") - date("Y", $startDate)) : 0);									
											for ($i = 10; $i >= 0; $i--)
												echo '<option'.($y == $i ? ' selected="selected"' : '').'>'.(date('Y')-$i).'</option>';
											?>	
										</select>
									</td>
									<td rowspan="2" align="left" class="menuBoxHeading">
										<?php echo REPORT_DETAIL; ?><br />
										<select name="detail" size="1">
											<option value="0"<?php if($srDetail == 0) echo "selected"; ?>><?php echo DET_HEAD_ONLY; ?></option>
											<option value="1"<?php if($srDetail == 1) echo " selected"; ?>><?php echo DET_DETAIL; ?></option>
											<option value="2"<?php if($srDetail == 2) echo " selected"; ?>><?php echo DET_DETAIL_ONLY; ?></option>
										</select><br />
										
										<?php echo REPORT_MAX; ?><br />
										<select name="max" size="1">
											<option value="0"><?php echo REPORT_ALL; ?></option>
											<option<?php if($srMax == 1) echo " selected"; ?>>1</option>
											<option<?php if($srMax == 3) echo " selected"; ?>>3</option>
											<option<?php if($srMax == 5) echo " selected"; ?>>5</option>
											<option<?php if($srMax == 10) echo " selected"; ?>>10</option>
											<option<?php if($srMax == 25) echo " selected"; ?>>25</option>
											<option<?php if($srMax == 50) echo " selected"; ?>>50</option>
										</select>
									</td>
									<td rowspan="2" align="left" class="menuBoxHeading">
										<?php echo REPORT_STATUS_FILTER; ?><br />
										<select name="status" size="1">
											<option value="0"><?php echo REPORT_ALL; ?></option>
											<?php
											foreach ($sr->status as $value) {
												echo '	<option value="'.$value['orders_status_id'].'"'.($srStatus == $value['orders_status_id'] ? ' selected="selected"' : '').'>
															'.$value['orders_status_name'].'
														</option>';
											} ?>
										</select><br />
										<?php
										$payments_arr = explode(';', MODULE_PAYMENT_INSTALLED);
										if($payments_arr[0] !='') { 
											echo REPORT_PAYMENT_FILTER; ?><br />
											<select name="payment" size="1">
											  	<option value="0"<?php echo (isset($_GET['payment']) &&  $_GET['payment'] == 0 ? ' selected="selected"' : '')?>><?php echo REPORT_ALL; ?></option>
												<?php
													#for($i=0; $n < count($payments), $i < $n; $i++){
													foreach($payments_arr AS $payments) {
														require(DIR_FS_LANGUAGES.$_SESSION['language'].'/modules/payment/'.$payments);	
														$payment = substr($payments, 0, strrpos($payments, '.'));
														$payment_text = constant(MODULE_PAYMENT_.strtoupper($payment)._TEXT_TITLE);
														echo '<option value="'.$payment.'"'.(isset($_GET['payment']) && $_GET['payment'] == $payment ? ' selected="selected"' : '').'>'.$payment_text.'</option>';
													 }
											echo '</select><br />';
										}
										?>
	                    
	                  				</td>
									<td rowspan="2" align="left" class="menuBoxHeading">
										<?php 
										#echo REPORT_EXP;
										?><br />
										<input type="hidden" name="export" value="0" />
										<!--select name="export" size="1">
										<option value="0" selected><?php echo EXP_NORMAL; ?></option>
										<option value="1"><?php echo EXP_HTML; ?></option>
										<option value="2"><?php echo EXP_CSV; ?></option>
										</select--><br />
										<?php echo REPORT_SORT; ?><br />
										<select name="sort" size="1">
											<option value="0"<?php if($srSort == 0) echo " selected"; ?>><?php echo SORT_VAL0; ?></option>
											<option value="1"<?php if($srSort == 1) echo " selected"; ?>><?php echo SORT_VAL1; ?></option>
											<option value="2"<?php if($srSort == 2) echo " selected"; ?>><?php echo SORT_VAL2; ?></option>
											<option value="3"<?php if($srSort == 3) echo " selected"; ?>><?php echo SORT_VAL3; ?></option>
											<option value="4"<?php if($srSort == 4) echo " selected"; ?>><?php echo SORT_VAL4; ?></option>
											<option value="5"<?php if($srSort == 5) echo " selected"; ?>><?php echo SORT_VAL5; ?></option>
											<option value="6"<?php if($srSort == 6) echo " selected"; ?>><?php echo SORT_VAL6; ?></option>
										</select><br />
									</td>
	                			</tr>
	                			<tr>
	                  				<td class="menuBoxHeading">
										<?php echo REPORT_END_DATE; ?><br />
										<select name="endD" size="1" style="width:auto">
										<?php 
											$j = ($endDate ? date("j", $endDate - 60* 60 * 24) : date("j"));
											for($i = 1; $i < 32; $i++)
												echo '<option'.($j == $i ? ' selected="selected"' : '').' value="'.$i.'">'.$i.'</option>';
											?>
										</select>
										<select name="endM" size="1" style="width:auto">
											<?php
											$m = ($endDate ? date("n", $endDate - 60* 60 * 24) : date("n"));
											for($i = 1; $i < 13; $i++)
												echo '<option'.($m == $i ? ' selected="selected"' : '').' value="'.$i.'">'.strftime("%B", mktime(0, 0, 0, $i, 1)).'</option>';
											?>
										</select>
										<select name="endY" size="1" style="width:auto">
											<?php
											$y = ($endDate ? date("Y") - date("Y", $endDate - 60* 60 * 24) : 0);
											for($i = 10; $i >= 0; $i--)
												echo '<option'.($y == $i ? ' selected="selected"': '').'>'.(date("Y")-$i).'</option>';
											?>
										</select>
	                  				</td>
	                			</tr>
	                			<tr>
									<td colspan="5" class="menuBoxHeading" align="right">
										<?php echo '<button type="submit" class="button">'.BUTTON_UPDATE.'</button>'; ?>
									</td>
								</tr>	
	              			</table>
	            		</form>
	          		</td>
				</tr>
				<tr>
					<td width=100% valign=top>
			            <table border="0" width="100%" cellspacing="0" cellpadding="2">
							<tr>
								<td valign="top">
									<table border="0" width="100%" cellspacing="0" cellpadding="0" class="dataTable">
					                    <tr class="dataTableHeadingRow">
											<td class="dataTableHeadingContent" align="right"><?php echo TABLE_HEADING_DATE; ?></td>
											<td class="dataTableHeadingContent" align="right"><?php echo TABLE_HEADING_ORDERS;?></td>
											<td class="dataTableHeadingContent" align="right"><?php echo TABLE_HEADING_ITEMS; ?></td>
											<td class="dataTableHeadingContent" align="right"><?php echo TABLE_HEADING_REVENUE;?></td>
											<td class="dataTableHeadingContent" align="right"><?php echo TABLE_HEADING_SHIPPING;?></td>
					                    </tr>
										<?php
										$sum = 0;
										$sum_total = 0;
										$ship_total = 0;
										$ord_total = 0;
										while($sr->actDate < $sr->endDate) {
											$info = $sr->getNext();
											$last = sizeof($info)-1;
										
											?>
											<tr class="dataTableRow" onmouseover="this.className='dataTableRowOver';this.style.cursor='default'" onmouseout="this.className='dataTableRow'">
												<?php
												switch ($srView) {
												case '3':
													echo '<td align="right" style="border-top:2px solid #C4C4C4"><b>'.date_long(date("Y-m-d H:i:s", $sr->showDate))." - ".date_short(date("Y-m-d H:i:s", $sr->showDateEnd)).'</b></td>';
													break;
					
												case '4':
													echo '<td align="right" style="border-top:2px solid #C4C4C4"><b>'.date_long(date("Y-m-d H:i:s", $sr->showDate)).'</b></td>';
													break;
												
												default;
													echo '<td align="right" style="border-top:2px solid #C4C4C4"><b>'.date_short(date("Y-m-d H:i:s", $sr->showDate))." - ".date_short(date("Y-m-d H:i:s", $sr->showDateEnd)).'</b></td>';
													break;
												} ?>
												<td align="right" style="border-top:2px solid #C4C4C4">
													<?php 
														echo $info[0]['order'];
														$ord_total += $info[0]['order'];
													?>
												</td>
												<td align="right" style="border-top:2px solid #C4C4C4"><?php echo $info[$last]['totitem'];?></td>
												<td align="right" style="border-top:2px solid #C4C4C4">
													<?php 
														echo $currencies->format($info[$last]['totsum']); 
														$sum_total += $info[$last]['totsum'];
													?>
												</td>
												<td align="right" style="border-top:2px solid #C4C4C4">
													<?php 
														echo ($info[0]['shipping'] > 0 ? $currencies->format($info[0]['shipping']) : '-');
														$ship_total += $info[0]['shipping'];
													?>
												</td>
						                    </tr>
											<?php
											  
											if($srDetail) {
											  	foreach($info AS $item) {
											  		 if($srMax == 0 OR $i < $srMax) { ?>
														<tr class="dataTableRow" onmouseover="this.className='dataTableRowOver';this.style.cursor='default'" onmouseout="this.className='dataTableRow'">
															<td>&nbsp;</td>
															<td align="left">
																<a href="<?php echo catalog_href_link("product_info.php?products_id=".$item['pid']) ?>" target="_blank"><?php echo $item['pmodel'].' : '.$item['pname']; ?></a><br />
															<?php
															if(is_array($item['attr'])) {
																$attr_info = $item['attr'];
																foreach ($attr_info as $attr) {
																	echo '<em>'.$attr['quant'].'x ' ;
																	$flag = 0;
																	foreach ($attr['options_values'] as $value) {
																		if($flag > 0)
																			echo ",".$value;
																		else {
																			echo $value;
																			$flag = 1;
																		}
																	}
																	$price = 0;
																	foreach ($attr['price'] AS $value)
																		$price += $value;
																	
																	echo ($price != 0 ? ' ('.($price > 0 ? '+' : '').$currencies->format($price).')' : '').'</em><br />';
																}
															} ?>
															</td>
															<td align="right"><?php echo $item['items_cnt']; ?>x</td>
															<?php echo '<td align="right">'.($srDetail == 2 ? $currencies->format($item['psum']) : '').'</td>';	?>
															<td>&nbsp;</td>
														</tr>
														<?php
											  		 }
											  	}
											}
										}
										?>
										<tr>
											<td align="right" colspan="2">
												<strong>Gesamt:</strong>
											</td>
											<td align="right">
												<strong>
													<?php echo $ord_total ?>
												</strong>
											</td>
											<td align="right">
												<strong>
												<?php
													echo ($sum_total > 0 ? $currencies->format($sum_total) : '-');
												?>
												</strong>
											</td>
											<td align="right">
												<strong>
												<?php
													echo ($ship_total > 0 ? $currencies->format($ship_total) : '-');
												?>
												</strong>
											</td>
					                    </tr>
									</table>
				                </td>
							</tr>
			            </table>
		          	</td>
		        </tr>
			</table>
	    </td>
  	</tr>
</table>
</div>
<?php require(DIR_WS_INCLUDES.'footer.php'); ?>
</body>
</html>
<?php require(DIR_WS_INCLUDES.'application_bottom.php');